package com.powernode.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.shop.domain.Prod;
import com.powernode.shop.domain.ProdTagReference;
import com.powernode.shop.domain.Sku;
import com.powernode.shop.mapper.ProdMapper;
import com.powernode.shop.service.ProdService;
import com.powernode.shop.service.ProdTagReferenceService;
import com.powernode.shop.service.SkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author ZHAOZHENYU
* @since 2022/10/12
*/
@Service
public class ProdServiceImpl extends ServiceImpl<ProdMapper, Prod> implements ProdService{

    @Autowired
    private ProdMapper prodMapper;

    @Autowired
    private ProdTagReferenceService prodTagReferenceService;

    @Autowired
    private SkuService skuService;

    @Override
    public Page<Prod> loadProdPage(Page<Prod> page, Prod prod) {

        return prodMapper.selectPage(page, new LambdaQueryWrapper<Prod>()
                .eq(!ObjectUtils.isEmpty(prod.getStatus()), Prod::getStatus, prod.getStatus())
                .like((StringUtils.hasText(prod.getProdName())), Prod::getProdName, prod.getProdName())
                .orderByDesc(Prod::getCreateTime)
        );

    }

    /**
     * 重写save
     * @param prod
     * @return
     */
    @Override
    @Transactional
    public boolean save(Prod prod) {
        prod.setShopId(1L);
        prod.setSoldNum(0);
        prod.setVersion(0);
        prod.setCreateTime(new Date());
        prod.setUpdateTime(new Date());
        // 上架时间
        if (prod.getStatus().equals(1)) {
            prod.setPutawayTime(new Date());
        }
        // 1、插入商品记录
        int num = prodMapper.insert(prod);
        if (num == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        // 2、插入标签
        List<Long> tagList = prod.getTagList();
        boolean tagFlag = false;
        if (!CollectionUtils.isEmpty(tagList)){
            // 中间表操作
            List<ProdTagReference> prodTagReferenceList = new ArrayList<>();
            tagList.forEach(tagId->{
                ProdTagReference prodTagReference = new ProdTagReference();
                prodTagReference.setShopId(1L);
                prodTagReference.setCreateTime(new Date());
                prodTagReference.setProdId(prod.getProdId());
                prodTagReference.setTagId(tagId);
                prodTagReference.setStatus(true);

                prodTagReferenceList.add(prodTagReference);
            });
            tagFlag = prodTagReferenceService.saveBatch(prodTagReferenceList);
            if (!tagFlag) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        // 3、插入SHU表
        List<Sku> skuList = prod.getSkuList();
        boolean skuFlag = false;
        if (!CollectionUtils.isEmpty(skuList)){
            skuList.forEach(sku->{
                sku.setProdId(prod.getShopId());
                sku.setRecTime(new Date());
                sku.setUpdateTime(new Date());
                sku.setStatus(1);
                sku.setIsDelete(0);
                sku.setVersion(0);
                sku.setActualStocks(sku.getStocks());
            });
            skuFlag = skuService.saveBatch(skuList);
            if (!skuFlag){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }

        return num > 0 && tagFlag && skuFlag;
    }


}
